package org.example.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.example.bean.User;
import org.example.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping("/user")
@Tag(name = "用户管理",description = "实现对用户的增删改查工作")
public class UserController {
    public static final int USER_STATUS_ACTIVE= 1;
    public static final int USER_STATUS_INACTIVE= 0;
    @Autowired
    private UserService userService;
    @PostMapping("/register")
    @Operation(summary = "用户注册",description = "添加一个新用户")
    public ResponseEntity<ResponseResult> register(@RequestParam("username") String username,
                                                  @RequestParam("userpwd") String userpwd,
                                                  @RequestParam("phone") String phone,
                                                  @RequestParam("email") String email,
                                                  @RequestParam(value = "gender", required = false, defaultValue = "0") int gender,
                                                  @RequestPart("avatarImgFile") MultipartFile avatarImgFile){
        User user = new User();
        user.setUsername(username);
        user.setUserpwd(userpwd);
        user.setPhone(phone);
        user.setEmail(email);
        user.setGender(gender);
        user.setAvatarImgFile(avatarImgFile);
        user.setUser_status(USER_STATUS_ACTIVE);
        userService.register(user);
        user.setAvatarImgFile(null);
        return ResponseEntity.ok(ResponseResult.builder().code(1).msg("注册成功").data(user).build());
    }

    @PostMapping("/login")
    @Operation(summary = "用户登录",description = "用户登录")
    public ResponseEntity<ResponseResult> login(@RequestParam("username") String username,
                                @RequestParam("userpwd") String userpwd){
        User user = new User();
        user.setUsername(username);
        user.setUserpwd(userpwd);
        User user1 = userService.login(user);
        return ResponseEntity.ok(ResponseResult.builder().code(1).msg("登录成功").data(user1).build());
    }

    //根据用户名查找用户信息
    @GetMapping("/findByUsername/{username}")
    @Operation(summary = "根据用户名查找用户信息",description = "根据用户名查找用户信息")
    public ResponseEntity<ResponseResult> getUserInfo(@PathVariable("username") String username){
        User user = userService.findByUsername(username);
        return ResponseEntity.ok(ResponseResult.builder().code(1).msg("查询成功").data(user).build());
    }

    //根据用户id修改用户信息
    @PutMapping("/updateUser/{user_id}")
    @Operation(summary = "根据用户id修改用户信息",description = "根据用户id修改用户信息")
    public ResponseEntity<ResponseResult> updateUser(@PathVariable("user_id") Integer user_id,
                                                     @RequestParam("username") String username,
                                                     @RequestParam("phone") String phone,
                                                     @RequestParam("email") String email,
                                                     @RequestPart("avatarImgFile") MultipartFile avatarImgFile,
                                                     @RequestParam(value = "gender", required = false, defaultValue = "0") int gender){
        User user = new User();
        user.setUsername(username);
        user.setPhone(phone);
        user.setEmail(email);
        user.setGender(gender);
        user.setAvatarImgFile(avatarImgFile);
        User user1 = userService.updateUser(user_id, user);
        user1.setAvatarImgFile(null);
        return ResponseEntity.ok(ResponseResult.builder().code(1).msg("修改成功").data(user1).build());
    }
    //根据状态查找用户
    @GetMapping("/findByStatus/{user_status}")
    @Operation(summary = "根据状态查找用户",description = "根据状态查找用户")
    public ResponseEntity<ResponseResult> findByStatus(@PathVariable("user_status") Integer user_status){
        List<User> users = userService.findByStatus(user_status);
        return ResponseEntity.ok(ResponseResult.builder().code(1).msg("查询成功").data(users).build());
    }

    //根据用户id修改用户状态
    @PutMapping("/updateUserStatus/{user_id}/{user_status}")
    @Operation(summary = "根据用户id修改用户状态",description = "根据用户id修改用户状态")
    public ResponseEntity<ResponseResult> updateUserStatus(@PathVariable("user_id") Integer user_id,
                                                           @PathVariable("user_status") Integer user_status){
        User user = userService.updateUserStatus(user_id, user_status);
        return ResponseEntity.ok(ResponseResult.builder().code(1).msg("修改成功").data(user).build());
    }

}
